/*
 * @Descripttion: 解释器模式
 * @Author: lcx
 * @version: v01
 * @Date: 2023-10-18 23:02:55
 * @LastEditors: lcx
 * @LastEditTime: 2023-10-19 23:58:29
 */

import java.util.HashSet;
import java.util.Set;

interface Expression {
    public boolean interpreter(String info);
}

class Context {
    private String[] regions = { "A区", "B区", "C区" };
    private String[] persons = { "开发人员", "测试人员", "运维人员" };

    NoTerminalExpression noTerminalExpression;

    Context() {
        TerminalExpression region = new TerminalExpression(regions);
        TerminalExpression person = new TerminalExpression(persons);
        noTerminalExpression = new NoTerminalExpression(region, person);
    }

    void check(String info) {
        boolean flag = noTerminalExpression.interpreter(info);
        if (flag)
            System.out.println("success");
        else
            System.out.println("error");
    }
}

class NoTerminalExpression implements Expression {

    TerminalExpression preson;
    TerminalExpression region;

    NoTerminalExpression(TerminalExpression preson,TerminalExpression region) {
        this.preson = preson;
        this.region = region;
    }

    @Override
    public boolean interpreter(String info) {
        String[] string = info.split("的");
        return preson.interpreter(string[0]) && region.interpreter(string[1]);
    }
}

class TerminalExpression implements Expression {
    public Set<String> set = new HashSet<String>();

    TerminalExpression(String[] string) {
        for (String s : string) {
            set.add(s);
        }
    }

    @Override
    public boolean interpreter(String info) {
        return set.contains(info);
    }

}

class test {
    public static void main(String[] args) {
        Context context = new Context();
        context.check("A区的开发人员");
        context.check("B区的开发人员");
        context.check("C区的开发人员");

    }
}